In [1]:
from IPython.display import SVG

In [2]:
bit="""<svg 
  version="1.1" 
  width="400px" 
  height="80"  
  xmlns="http://www.w3.org/2000/svg" 
  xmlns:xlink="http://www.w3.org/1999/xlink"  
  xmlns:ev="http://www.w3.org/2001/xml-events"
 >
 <g transform="translate(75,60)">
  <g fill="deepskyblue" stroke="black" stroke-width="1px">
   <desc>Marble</desc>
   <a xlink:href="../statistics/s81/marblerace.html" xlink:title="Marble race">
   <circle cx="0" cy="0" r="15" /> 
   </a>
  </g>
 </g>

 <g transform="translate(100,50)">
 <switch>
 <foreignObject width="100" height="40">
  <math xmlns="http://www.w3.org/1998/Math/MathML">
  <mtext>KE</mtext>
  <mo>=</mo>
  <mfrac>
   <mrow><mn>1</mn></mrow>
   <mrow><mn>2</mn></mrow>
  </mfrac>
  <mi>m</mi><msup><mi>v</mi><mn>2</mn></msup>
 </math>
 </foreignObject>
 </switch>
 </g>
 
 <g transform="translate(200,40)">
  <g stroke="black" stroke-width="1" fill="lavender">
   <desc>Linear arrow for kinetic energy</desc>
   <a xlink:href="../physci/ps81/q03.xhtml" xlink:title="Kinetic energy on a quiz">
   <polygon points="0,10 30,10 30,0 50,20 30,40 30,30 0,30" />
   </a>
  </g>
 </g> 
 
 <g transform="translate(0,0)">
 <switch>
 <foreignObject width="100" height="50">
 <math xmlns="http://www.w3.org/1998/Math/MathML">
  <mtext>RE</mtext>
  <mo>=</mo>
  <mrow>
  <mfrac>
   <mrow><mn>1</mn></mrow>
   <mrow><mn>2</mn></mrow>
  </mfrac>
  <mi>I</mi><msup><mi>&#x03C9;</mi><mn>2</mn></msup>
  </mrow>
 </math>
 </foreignObject>
 </switch>
 </g>
 
 <!-- Again, SVG after MathMl label -->
 <g transform="translate(65,20) scale(2.0)">
  <g stroke="black" stroke-width="1" fill="lime">
   <desc>Curved arrow for rotational energy</desc>
   <a xlink:href="../physci/ps73/lab04_2008.html#rotational_notes" xlink:title="Rotational end notes">
   <path 
   d="M 0,0 A 25,25 0 0,1 17.68,7.32 l 1.41,-1.41 v 6.36 h -6.36 l 1.41,-1.41 A 20,20 0 0,0 0,5 L 0,0 z" 
   />
   </a>
  </g>
 </g>

</svg>"""

In [3]:
SVG(bit)


Out[3]:
Marble KE = 1 2 mv2 Linear arrow for kinetic energy RE = 1 2 Iω2 Curved arrow for rotational energy

In [3]:


In [1]:
#Path = SVG_lib.py



from collections import OrderedDict as Dict

class point(object):
    def __init__(self, x = 0, y = 0):
        self.x = x
        self.y = y
    
    def __repr__(self):
        txt = 'class: point(x = %g, y = %g)'%(self.x, self.y)
        return txt
    
    def __iter__(self):
        yield [self.x,self.y]
    
    def offset_y(self,val):
        return point(self.x,self.y+val)
    def offset_x(self,val):
        return point(self.x+val,self.y)
    def List(self):
        return [self.x, self.y]



colors = Dict()

with open('SVG_colors.txt','r') as f:
    for i, line in enumerate(f):
        if i==0:
            continue
        line = line.strip()
        parts = line.split('\t')
        pieces = []
        for part in parts:
            p = part.strip()
            if len(p)==0:
                continue
            pieces.append(part)
        colors[pieces[0].strip()]=pieces

class Line(object):
    def __init__(self, p0=point(0,0), p1=point(100,100)):
        """p0, and p1 must be passed in as point class memebers or named tuples with x and y"""
        self.p0 = p0
        self.p1 = p1
        self.color = 'black'
        self.width = 1
        
    def svg_line_txt(self):
        txt  = '<line x1="%g" y1="%g" '%(self.p0.x, self.p0.y)
        txt += 'x2="%g" y2="%g" '%(self.p1.x, self.p1.y)
        txt += 'style="stroke:%s;'%self.color
        txt += 'stroke-width:%d" />'%self.width
        return txt

    @property
    def svg(self):
        txt = '<svg>'
        txt += '<line x1="%g" y1="%g" '%(self.p0.x, self.p0.y)
        txt += 'x2="%g" y2="%g" '%(self.p1.x, self.p1.y)
        txt += 'style="stroke:%s;'%self.color
        txt += 'stroke-width:%d" />'%self.width
        txt += '</svg>'
        return txt
    
    def _repr_svg_(self):
        return self.svg
    
    @property
    def color(self):
        return self._color
    @color.setter
    def color(self, value):
        value = value.lower()
        check = False
        if value in colors.keys():
            #name
            self._color = value
            check = True
        elif ',' in value:
            #rgb
            try:
                r,g,b = value.split(',')
                self._color = 'rgb(%d,%d,%d)'%(r,g,b)
                check = True
            except:
                pass
        elif 6<=len(value)<=7:
            #hex
            value = value.strip('#')
            try:
                r = int(value[0:2],16)
                g = int(value[2:4],16)
                b = int(value[4:6],16)
                self._color = 'rgb(%d,%d,%d)'%(r,g,b)
                check = True
            except:
                pass
        if not check:
            er = '%s is not a valid SVG color'%value
            raise(ValueError(er))
    
    @property
    def colors(self):
        print(', '.join(colors.keys()))
    
    @property
    def length(self):
        x0,y0 = self.p0.List()
        x1,y1 = self.p1.List()
        return ((x1-x0)**2+(y1-y0)**2)**0.5
    @property
    def direction(self):
        x0,y0 = self.p0.List()
        x1,y1 = self.p1.List()
        return [(x1-x0),(y1-y0)]

class arrow(Line):
    def __init__(self, p0=point(0,0), p1=point(100,100)):
        Line.__init__(self, p0, p1)
    
    @property
    def svg(self):
        txt = '<svg viewBox = "0 0 400 200"> '
        txt += '  <defs>'
        txt += '    <marker id="head" orient="auto" markerWidth="50" markerHeight="100"'
        txt += '            refX="0.25" refY="5">'
        txt += '      <path d="M0,0 V10 L5,5 " fill="red" />'
        txt += '    </marker>'
        txt += '  </defs>    '
        txt += '  <path'
        txt += '    marker-end="url(#head)"'
        txt += '    stroke-width="%d" fill="none" stroke=%s  '%(self.width, self.color)
        txt += '    d="M%d,%d '%(self.p0.x, self.p0.y)
        txt += 'L%d,%d"'%(self.p1.x, self.p1.y)
        txt += '    />    '
#         txt += '<line x1="%d" y1="%d" '%(self.p0.x, self.p0.y)
#         txt += 'x2="%d" y2="%d" '%(self.p1.x, self.p1.y)
#         txt += 'marker-end=url(#head); '
#         txt += 'style="stroke:%s;'%self.color
#         txt += 'stroke-width:%d" />'%self.width
        txt += '</svg>'
        return txt
    
    def _repr_svg_(self):
        return self.svg


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-1-a38461c6d554> in <module>()
     33         colors[pieces[0].strip()]=pieces
     34 
---> 35 class line(object):
     36     def __init__(self, p0=P0, p1=P1):
     37         """p0, and p1 must be passed in as point class memebers or named tuples with x and y"""

<ipython-input-1-a38461c6d554> in line()
     34 
     35 class line(object):
---> 36     def __init__(self, p0=P0, p1=P1):
     37         """p0, and p1 must be passed in as point class memebers or named tuples with x and y"""
     38         self.p0 = p0

NameError: name 'P0' is not defined

In [ ]:
P0 = point(0,0)
P1 = point(100,100)

In [6]:
P0


Out[6]:
class: point(x = 0, y = 0)

In [7]:
aa = arrow()
# aa.svg.split(' ')
SVG(aa.svg)
# print(aa.svg.replace('>','>\n'))


Out[7]:

In [8]:
ttt="""<svg viewBox = "0 0 400 200">
      <path d = "M 0 0 L 100 100 " fill = "none" stroke = "black" stroke-width = "2" />
</svg>"""
SVG(aa.svg)


Out[8]:

In [8]:


In [9]:
tx = """<svg xmlns="http://www.w3.org/2000/svg" viewBox="-500 -1000 2000 2000">
  <defs>
    <marker id='head' orient='auto' markerWidth='2' markerHeight='4'
            refX='0.1' refY='2'>
      <path d='M0,0 V4 L2,2 Z' fill='red' />
    </marker>
  </defs>    
  <path
    marker-end='url(#head)'
    stroke-width='5' fill='none' stroke='black'  
    d='M0,0 C45,45 45,-45 90,0'
    />    
</svg>
"""
# SVG(tx)
lines = tx.split('\n')
for line in lines:
    print("txt += '" + line.replace("'",'"') +"'")


txt += '<svg xmlns="http://www.w3.org/2000/svg" viewBox="-500 -1000 2000 2000">'
txt += '  <defs>'
txt += '    <marker id="head" orient="auto" markerWidth="2" markerHeight="4"'
txt += '            refX="0.1" refY="2">'
txt += '      <path d="M0,0 V4 L2,2 Z" fill="red" />'
txt += '    </marker>'
txt += '  </defs>    '
txt += '  <path'
txt += '    marker-end="url(#head)"'
txt += '    stroke-width="5" fill="none" stroke="black"  '
txt += '    d="M0,0 C45,45 45,-45 90,0"'
txt += '    />    '
txt += '</svg>'
txt += ''

tx = """ """


In [10]:
SVG(aa.svg)


Out[10]:

In [10]:


In [10]:


In [11]:
r_line= line()
r_line.color = 'ff0fff'
SVG(r_line.svg)


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-11-2c5cd02450ca> in <module>()
----> 1 r_line= line()
      2 r_line.color = 'ff0fff'
      3 SVG(r_line.svg)

TypeError: 'str' object is not callable

In [ ]:
print(r_line.svg)

In [ ]:
r_line.colors

In [ ]:
r_line.p0

In [ ]: